package com.hualongdata.util;

import com.hualongdata.util.scalaapi.Utils$;
import org.apache.commons.codec.digest.DigestUtils;
import scala.Tuple2;

import java.util.Collection;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * Created by yangbajing(yangbajing@gmail.com) on 2016-08-31.
 */
public class Utils {

    public static String randomString(int n) {
        return Utils$.MODULE$.randomString(n);
    }

    public static Tuple2<String, String> generateSaltAndPassword(String password) {
        return Utils$.MODULE$.generateSaltAndPassword(password);
    }

    public static String saltSecretPassword(String salt, String password) {
        return Utils$.MODULE$.saltSecretPassword(salt, password);
    }

    public static boolean matchPassword(String salt, String encryptedPassword, String originalPassword) {
        return Utils$.MODULE$.matchPassword(salt, encryptedPassword, originalPassword);
    }

    public static <T> String streamJoinString(Stream<T> stream) {
        return collectionJoinString(stream.collect(Collectors.toList()));
    }

    public static String fileSha256(byte[] bytes) {
        return DigestUtils.sha256Hex(bytes);
    }

    public static <T> String collectionJoinString(Collection<T> list) {
        return collectionJoinString(list, ",");
    }

    public static <T> String collectionJoinString(Collection<T> list, CharSequence sep) {
        StringBuilder sb = new StringBuilder();
        boolean start = true;
        for (T item : list) {
            if (start) {
                sb.append(item);
                start = false;
            } else {
                sb.append(sep);
                sb.append(item);
            }
        }
        return sb.toString();
    }

    public static int parseInt(Object o) {
        return Utils$.MODULE$.parseInt(o);
    }

    public static int parseInt(Object o, int deft) {
        return Utils$.MODULE$.parseInt(o, deft);
    }

    public static long parseLong(Object o, long deft) {
        return Utils$.MODULE$.parseLong(o, deft);
    }
}
